package com.iotimc.attendancesystem.base;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import butterknife.ButterKnife;
import butterknife.Unbinder;


/**
 * Created by david on 2017/1/13.
 * @author david
 */

public abstract class BaseFragment extends Fragment {
    Unbinder unbinder;
    /**
     * 获取资源layout文件
     * @return
     */
    protected abstract int getResId();

    /**
     * 缓存Fragment view
     */
    protected View rootView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = inflater.inflate(getResId(), container,false);
            unbinder =  ButterKnife.bind(this,rootView);
            initView(rootView);

        }
        //缓存的rootView需要判断是否已经被加过parent， 如果有parent需要从parent删除，要不然会发生这个rootview已经有parent的错误。
        ViewGroup parent = (ViewGroup) rootView.getParent();
        if (parent != null) {
            parent.removeView(rootView);
        }
        return rootView;
    }

    protected void initView(View view) {

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
